﻿// -----------------------------------------------------------------------
// <copyright file="Token.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Scanner
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    public enum TokenType
    {
        EndFile, Error,Comment,
        If, Then, Else, End, Repeat, Until, Read, Write, //Reserved Words
        ID, Num, //multicharacher tokens
        Assign, Equal, LessThan, Plus, Minus, Times, LeftParentheses, RightParentheses, SemiColon //special symbols
    }
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Token
    {
        TokenType tokenType;

        public TokenType TokenType
        {
            get { return tokenType; }
            set { tokenType = value; }
        }
        public string Lexem;

        public Token(string finalName, string lexem)
        {
            TokenType t = new TokenType();

            if (finalName == "identifer")
                t = TokenType.ID;
            else if (finalName == "number")
                t = TokenType.Num;
            else if (finalName == "comment")
                t = TokenType.Comment;
            else if (finalName == "\0")
                t = TokenType.EndFile;
            else if (finalName == "+")
                t = TokenType.Plus;
            else if (finalName == "-")
                t = TokenType.Minus;
            else if (finalName == "*")
                t = TokenType.Times;
            else if (finalName == "=")
                t = TokenType.Equal;
            else if (finalName == ")")
                t = TokenType.RightParentheses;
            else if (finalName == "(")
                t = TokenType.LeftParentheses;
            else if (finalName == "<")
                t = TokenType.LessThan;
            else if (finalName == ":=")
                t = TokenType.Assign;
            else if (finalName == "\0")
                t = TokenType.EndFile;
            else if (finalName == ";")
                t = TokenType.SemiColon;
            else if (finalName == "error")
                t = TokenType.Error;

            string[] temp = lexem.Split(' ','\t', '\n');
            StringBuilder transformedString = new StringBuilder();
            foreach (string s in temp)
            {
                transformedString.Append(s);
            }
            //this.Lexem = lexem;
            this.Lexem = temp[temp.Length - 1];
            if (t == TokenType.ID)
            {
                switch (this.Lexem)
                {
                    case "if":
                        {
                            this.tokenType = TokenType.If;
                            this.Lexem = "";
                        } break;
                    case "then":
                        {
                            this.tokenType = TokenType.Then;
                            this.Lexem = "";
                        } break;
                    case "else":
                        {
                            this.tokenType = TokenType.Else;
                            this.Lexem = "";
                        } break;
                    case "end":
                        {
                            this.tokenType = TokenType.End;
                            this.Lexem = "";
                        } break;
                    case "repeat":
                        {
                            this.tokenType = TokenType.Repeat;
                            this.Lexem = "";
                        } break;
                    case "until":
                        {
                            this.tokenType = TokenType.Until;
                            this.Lexem = "";
                        } break;
                    case "read":
                        {
                            this.tokenType = TokenType.Read;
                            this.Lexem = "";
                        } break;
                    case "write":
                        {
                            this.tokenType = TokenType.Write;
                        } break;
                    default:
                        {
                            this.tokenType = TokenType.ID;
                        } break;
                }
            }
            else
            {
                tokenType = t;
            }

            
        }

        public string getToken()
        {
            return this.tokenType.ToString() + "|" + this.Lexem;
        }
    }
}
